package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.bean.CreditNumBean;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 我的信用值
 */
public class CreditNumActivity extends BaseActivity implements ActionBarClickListener, OnRefreshLoadmoreListener, BaseRecyclerAdapter.OnDataLoadFinish{

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.credit_number)
    TextView credit_number;
    @BindView(R.id.credit_number_results)
    TextView credit_number_results;
    @BindView(R.id.credit_num_refreshlayout)
    SmartRefreshLayout credit_num_refreshlayout;
    @BindView(R.id.recycler_recode)
    RecyclerView recyclerRecode;
    @BindView(R.id.empty_layout)
    LinearLayout empty;
    @BindView(R.id.tv_msg)
    TextView tv_msg;

    BaseRVAdapter baseRVAdapter;
    List<CreditNumBean.InfoBean> info;
    private int paging = 0;

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, CreditNumActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_num);
        ButterKnife.bind(this);

        actionbar.setData("我的信用值", R.drawable.ic_lefft_white, null, 0, "信用秘籍", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        setRefrash();
    }

    private void setRefrash() {
        credit_num_refreshlayout.setOnRefreshLoadmoreListener(this);
        credit_num_refreshlayout.setRefreshHeader(new ClassicsHeader(this));
        credit_num_refreshlayout.setRefreshFooter(new ClassicsFooter(this));
        getData();
    }

    private void getData() {
        showDialog("获取中...");
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("page",paging);
        map.put("type", 8); //Int  type    5免费商城   6返利商城   7拼团商城  8-共享商城
        XUtil.Post(Url.Ucenter_myCredit, map, new MyCallBack<String>() { //获取我的分享
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e("---我的信用值:  ", result);
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    if ("1".equals(re)) {
                        Gson gson = new Gson();
                        CreditNumBean creditNumBean = gson.fromJson(result, CreditNumBean.class);
                        credit_number.setText(creditNumBean.getCredit()); //信用值
                        credit_number_results.setText("信用值" + creditNumBean.getLevel()); //信用值成绩-良好
                        if (paging == 0){
                            if (null != info){
                                info.clear();
                            }
                            info = creditNumBean.getInfo();
                            credit_num_refreshlayout.finishRefresh();
                            credit_num_refreshlayout.resetNoMoreData();
                        }else {
                            credit_num_refreshlayout.finishLoadmore();
                            baseRVAdapter.notifyDataSetChanged();
                        }
                        setData();
                    }else if ("-1".equals(jsonObject.optString("re"))){
                        MyToast(getResources().getString(R.string.login_out));
                        MyApplication.getInstance().gotoLoginActivity();
                    }else {
                        if (paging == 0){
                            credit_num_refreshlayout.setVisibility(View.GONE);
                            empty.setVisibility(View.VISIBLE);
                            MyToast(jsonObject.optString("info"));
                            tv_msg.setText("暂无店铺");
                        }else {
                            credit_num_refreshlayout.finishLoadmore();
                            credit_num_refreshlayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                            empty.setVisibility(View.GONE);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private void setData() {
        recyclerRecode.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        baseRVAdapter = new BaseRVAdapter(CreditNumActivity.this, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_creditnum;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.tv_event, info.get(position).getWay_name());
                if("1".equals(info.get(position).getCredit_type())){
                    holder.setTextView(R.id.tv_number, "+" + info.get(position).getCredit_value());
                }else if("2".equals(info.get(position).getCredit_type())){
                    holder.setTextView(R.id.tv_number, "-" + info.get(position).getCredit_value());
                }
                holder.setTextView(R.id.tv_date, info.get(position).getPosttime());
            }
        };
        recyclerRecode.setAdapter(baseRVAdapter);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {
        startActivity(CreditCheatsActivity.createIntent(this));
    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                paging = paging + 1;
                getData();
            }
        },2000);
    }
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                paging = 0;
                getData();
            }
        },2000);
    }

    @Override
    public void loadDataFinished(int datasLength) {

    }
}
